//
//  SelectWebUrlViewController.m
//  ZhuiKe55like
//
//  Created by junseek on 15-4-13.
//  Copyright (c) 2015年 五五来客 李江. All rights reserved.
//

#import "SelectWebUrlViewController.h"
#import "MyWebView.h"
#import "BusinessCardUpdateViewController.h"
#import "V4ShareShowView.h"

@interface SelectWebUrlViewController ()<UIWebViewDelegate>{
    MyWebView *WebView;
    NSString *strTempUrl;
    UIBarButtonItem *LeftItem1;
    UIButton *btnBack;
    NSArray *arrayType;
    
    NSString *strTitle;
    NSString *strDescription;
    NSString *strImageUrl;
 
    BOOL boolBack;
    
    BOOL boolRefresh;
}


@end

@implementation SelectWebUrlViewController

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initComponents];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myWebView_nil:) name:@"myWebView_nil" object:nil];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (boolRefresh) {
        boolRefresh=NO;
        [WebView refreshWeb];
    }
    [SVProgressHUD dismiss];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [SVProgressHUD dismiss];
}

#pragma mark push_NSNotification
-(void)myWebView_nil:(NSNotification *)note{
    [WebView removeFromSuperview];
    WebView=nil;
}


- (void)initComponents{
    strTempUrl=@"";
    if (!kVersion7) {
        [self navbarTitle:@""];
        [self backButton];
    }
    
    btnBack=[RHMethods buttonWithFrame:CGRectMake(44, 20, 50,44 ) title:@"关闭" image:@"" bgimage:@""];
    [btnBack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnBack setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btnBack addTarget:self action:@selector(LeftButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    btnBack.titleLabel.font=Font(17);
    btnBack.hidden=YES;
    [self.navView addSubview:btnBack];
    
    
    
    
    WebView = [[MyWebView alloc] initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth,kContentHeight)];
    WebView.delegate=self;
    [self.view addSubview:WebView];
    WebView.scalesPageToFit=YES;
    [WebView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    
   strTempUrl=[self.otherInfo valueForJSONStrKey:@"url"];
    if (strTempUrl.length>4) {
        strTempUrl= [[[strTempUrl lowercaseString] substringToIndex:4] isEqualToString:@"http"]?strTempUrl:[NSString stringWithFormat:@"%@%@",baseWebPath,strTempUrl];
    }
    
    [WebView loadMyWeb:strTempUrl];
   
    if ([self.userInfo isEqualToString:@"userProject"] ||
        [self.userInfo isEqualToString:@"userDownload"]||
        [self.userInfo isEqualToString:@"shareCard"]) {
        
        
        [self rightButton:nil image:@"shareicon" sel:@selector(shareBUttonClicked)];
        self.navrightButton.frame=CGRectMake(kScreenWidth-44, 20, 44, 44);
//        [self rightButton:nil image:@"headfx" sel:@selector(shareBUttonClicked)];
    }
    
}


#pragma mark button
-(void)shareBUttonClicked{
    //分享XKFV3personcardsharecard
    if ([self.userInfo isEqualToString:@"shareCard"]) {
        krequestParam
        [dictparam setValue:[self.otherInfo valueForJSONStrKey:@"uid"] forKey:@"byuid"];
        [NetEngine createPostAction:[NSString stringWithFormat:@"%@%@",XKFV3personcardsharecard,dictparam.wgetParamStr] withParams:dictparam onCompletion:^(id resData, BOOL isCache) {
            if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
                [self showShareViewData:[resData objectForJSONKey:@"data"]];
            }else{
                [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
            }
        }];
    }else{
        NSDictionary *dct=[self.otherInfo objectForJSONKey:@"sharedata"];
        [self showShareViewData:dct];
    }
    
}
-(void)showShareViewData:(NSDictionary *)dct{
    NSMutableDictionary *dic=[NSMutableDictionary new];
    [dic setValue:[dct valueForJSONStrKey:@"pic"] forKey:@"src"];
    [dic setValue:[dct valueForJSONStrKey:@"title"] forKey:@"title"];
    [dic setValue:[dct valueForJSONStrKey:@"content"] forKey:@"descr"];
    [dic setValue:[dct valueForJSONStrKey:@"url"] forKey:@"url"];
    
//    NSDictionary *dct=[self.otherInfo objectForJSONKey:@"sharedata"];
//    NSMutableDictionary *dic=[NSMutableDictionary new];
//    [dic setValue:[dct valueForJSONStrKey:@"pic"] forKey:@"src"];
//    [dic setValue:[dct valueForJSONStrKey:@"title"] forKey:@"title"];
//    [dic setValue:[dct valueForJSONStrKey:@"content"] forKey:@"descr"];
//    [dic setValue:[dct valueForJSONStrKey:@"url"] forKey:@"url"];
//    
    [V4ShareShowView showMeWithShareDic:dic WithCollectiondic:nil CallBackBlock:^(id data, int status, NSString *msg) {
        if (status==200) {
            if ([data isEqualToString:@"收藏"]) {
                //                 [self backRefreshData];
                //                        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
                //                            [self backRefreshData];
                //                        }else{
                //                //            _btnCollection.selected=!_btnCollection.selected;
                ////                            [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
                //                        }
            }else{
                
//                [self shareStatistics];
            }
        }
        
    }];
    
//    [[Utility Share] showShareData:dic suc:^(NSInteger NoSuc) {
//        if (NoSuc==1) {
//            [SVProgressHUD showSuccessWithStatus:@"分享成功！"];
//        }
//    }];

}
-(void)backButtonClicked:(id)sender{
//    [SVProgressHUD dismiss];
    if (WebView.canGoBack){
        //返回上一个网页
        [WebView goBack];
    }else{
        if (!boolBack) {
            boolBack=YES;
            [super backButtonClicked:nil];
        }
    }
}
-(void)LeftButtonClicked{
    if (!boolBack) {
        boolBack=YES;
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}
//分离URL参数
-(NSMutableDictionary *)dicSeparatedUrl:(NSString *)strurl{
    //获取问号的位置，问号后是参数列表
    NSRange range = [strurl rangeOfString:@"?"];
    NSLog(@"参数列表开始的位置：%d", (int)range.location);
    
    //获取参数列表
    NSString *propertys = [strurl substringFromIndex:(int)(range.location+1)];
    NSLog(@"截取的参数列表：%@", propertys);
    
    //进行字符串的拆分，通过&来拆分，把每个参数分开
    NSArray *subArray = [propertys componentsSeparatedByString:@"&"];
    NSLog(@"把每个参数列表进行拆分，返回为数组：n%@", subArray);
    
    //把subArray转换为字典
    //tempDic中存放一个URL中转换的键值对
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithCapacity:4];
    
    for (int j = 0 ; j < subArray.count; j++)
    {
        if ([subArray[j] notEmptyOrNull]) {
            //在通过=拆分键和值
            NSArray *dicArray = [subArray[j] componentsSeparatedByString:@"="];
            if ([dicArray count]>1) {
                //给字典加入元素
                [tempDic setObject:dicArray[1] forKey:dicArray[0]];
            }else if([dicArray count]){
                [tempDic setObject:@"" forKey:dicArray[0]];
            }
        }
    }
    NSLog(@"打印参数列表生成的字典：n%@", tempDic);
    
    return tempDic;
}
#pragma markUIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    strTempUrl= [request.URL absoluteString];
    DLog(@"request.URL:%@",strTempUrl);
    if ( [strTempUrl rangeOfString:@"&action=userEditCard"].length>0) {
        //编辑个人名片
        [self pushController:[BusinessCardUpdateViewController class] withInfo:@"" withTitle:@"名片编辑" withOther:nil];
        
        return NO;
    }
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
//    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeNone];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if (WebView.canGoBack) {
        self.backButton.frameWidth=44;
        btnBack.hidden=NO;
        UIView *viewTemp=[self.navView viewWithTag:102];
        viewTemp.frame=CGRectMake(80, Y(viewTemp), kScreenWidth-160, H(viewTemp));
        
    }else{
        self.backButton.frameWidth=60;
        UIView *viewTemp=[self.navView viewWithTag:102];
        btnBack.hidden=YES;
        viewTemp.frame=CGRectMake(60, Y(viewTemp), kScreenWidth-120, H(viewTemp));
    }
    
//    strDescription = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName(\"description\")[0].content"];
//    DLog(@"strDescription______:%@",strDescription);
//    strImageUrl= [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName(\"picture\")[0].content"];
//    DLog(@"strImageUrl______:%@",strImageUrl);
//    
//    NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
//    DLog(@"currentURL:%@",currentURL);
//    strTempUrl=currentURL;
//    
//    
//    NSString *elementsByName = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('share')[0].value"];
//    DLog(@"ElementsByName:%@",elementsByName);
//   
    
    strTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    if ([strTitle notEmptyOrNull]) {
        [self navbarTitle:strTitle];
    }    
    
//    [SVProgressHUD dismiss];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
//    [SVProgressHUD showImage:nil status:@"加载失败,下拉可以刷新界面"];
    DLog(@"error:%@",error);
}
#pragma mark pop
-(void)popRefreshData{
    boolRefresh=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
